package com.hmall.item.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.MQConstants;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {

    @Autowired(required = false)
    private ItemMapper itemMapper;

    //消息发布者，注入消息发布工具类RabbitTemplate
    @Autowired
    RabbitTemplate rabbitTemplate;


    /**
     * 分页查询商品
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageDTO<Item> list(Integer page, Integer size) {

        Page<Item> pageMP = new Page<>(page, size);
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        itemMapper.selectPage(pageMP, queryWrapper);


        PageDTO<Item> pageDTO = new PageDTO<>();
        pageDTO.setList(pageMP.getRecords());
        pageDTO.setTotal(pageMP.getTotal());
        // System.out.println("pageDTO = " + pageDTO);
        return pageDTO;
    }

    /**
     * 根据id查询商品
     *
     * @param id
     * @return
     */
    @Override
    public Item findById(Long id) {
        return itemMapper.selectById(id);
    }

    /**
     * 新增商品
     *
     * @param item
     */
    @Override
    public void addItem(Item item) {
        itemMapper.insert(item);
    }


    /**
     * 商品上架(1)，下架(2)
     *
     * @param id
     * @param status
     */
    @Override
    public void switStatu(Long id, Integer status) {
        // System.out.println("id = " + id);
        // System.out.println("status = " + status);
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        itemMapper.updateById(item);

        //更新完成后发送包含id的消息到交换机
        if (status != null && status.equals(1)) {//上架调用ea新增，消息路由key 是insert
            //发送消息
            rabbitTemplate.convertAndSend(MQConstants.ITEM_EXCHANGE, MQConstants.ITEM_INSERT_KEY, id);
        }
        if (status != null && status.equals(2)) {//下架调用ea删除，消息路由key ITEM_DELETE_KEY
            rabbitTemplate.convertAndSend(MQConstants.ITEM_EXCHANGE, MQConstants.ITEM_DELETE_KEY, id);
        }
    }

    /**
     * 修改商品
     *
     * @param item
     */
    @Override
    public void updateItem(Item item) {
        itemMapper.updateById(item);
    }

    /**
     * 根据id删除商品
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //下架的商品才能删除，前端已经做好这个逻辑判断
        itemMapper.deleteById(id);
    }

    /**
     * 获取记录总条数
     *
     * @return
     */
    @Override
    public Integer getTotal() {
        return itemMapper.selectCount(new QueryWrapper<>());
    }

    /**
     * 扣减库存接口
     *
     * @param itemId
     * @param num
     */
    @Override
    public void subStockByItemId(Long itemId, Integer num) {

        Item item = itemMapper.selectById(itemId);
        Integer stockDB = item.getStock();
        System.out.println("库存更新前item = " + item);


        item.setStock(stockDB - num);
        System.out.println("库存更新后item = " + item);

        itemMapper.updateById(item);

    }
}
